<?php
session_start();
include_once("../../untils/conn.php");
mysqli_query($con, "set names utf8");

// 获取当前正在登录的账号信息的代理
$proxy_acc = $_SESSION["account"];
$time = time();

// 引用验证身份证信息的函数文件
include_once("public/verifyidcard.php");

// 处理提交的重命名表单数据
if (isset($_POST['proxy_rename'])) {
    $proxy_rename = $_POST['proxy_rename'];
    $proxy_uid = $_POST['proxy_uid'];
    $proxy_phone = $_POST['proxy_phone'];
    $verification_code = $_POST['verification_code'];
    
    // 调试输出
    // echo "姓名: " . $proxy_rename . "<br>";
    // echo "身份证号: " . $proxy_uid . "<br>";
    // echo "手机号: " . $proxy_phone . "<br>";
    // echo "验证码: " . $verification_code . "<br>";
    // exit;
    

    
    // 验证身份证号格式
      if (!verifyIdCard($proxy_uid)) {
        $response = array('code' => -2, 'msg' => '身份证号有误');
        echo json_encode($response);
        exit;
    }

    // 查询验证码
    $query = "SELECT * FROM vercodes WHERE phone = '$proxy_phone' AND code = '$verification_code'";
    $result = mysqli_query($con, $query);
    $row = mysqli_fetch_assoc($result);

    if (!$row) {
        // 验证码不正确
        $response = array('code' => 3, 'msg' => '验证码不正确');
        echo json_encode($response);
        exit;
    }
    
    // 验证验证码有效期
    $sutime = $row['sutime'];
    if ($time - $sutime > 300) {
        // 验证码已过期
        $response = array('code' => 4, 'msg' => '验证码已过期');
        echo json_encode($response);
        exit;
    }
    
        // 查询system表中type为xitong的数据
    $query = "SELECT * FROM system WHERE type = 'xitong'";
    $result = mysqli_query($con, $query);
    $systemData = mysqli_fetch_assoc($result);

    if ($systemData) {
        // 取出auto字段的值
        $auto = $systemData['auto'];

        if ($auto == 1) {
            // auto等于1时，更新rename_table和proxy表的数据
            $query = "INSERT INTO rename_table (proxy_acc, `rename`, reuid, phone, sutime, status) VALUES ('$proxy_acc', '$proxy_rename', '$proxy_uid', '$proxy_phone', '$time', 1)";
            $result = mysqli_query($con, $query);

            $query = "UPDATE proxy SET proxy_rename='$proxy_rename', proxy_uid='$proxy_uid', proxy_phone='$proxy_phone', proxy_rename_sta=1 WHERE proxy_acc='$proxy_acc'";
            $result = mysqli_query($con, $query);
            
            if ($result) {
            // 更新成功
                $response = array(
                    'code' => 2,
                    'msg' => '实名成功！'
                );
                echo json_encode($response);
                exit;
            } else {
                // 更新失败
                $response = array(
                    'code' => -1,
                    'msg' => '系统出错，请稍后再试！'
                );
                echo json_encode($response);
                exit;
            }
        
        } else {
            // auto不等于1时，只执行插入操作
            $query = "INSERT INTO rename_table (proxy_acc, `rename`, reuid, phone, sutime) VALUES ('$proxy_acc', '$proxy_rename', '$proxy_uid', '$proxy_phone', '$time')";
            $result = mysqli_query($con, $query);
            
             if ($result) {
            // 插入成功
                $response = array(
                    'code' => 2,
                    'msg' => '提交成功，请等待审核！'
                );
                echo json_encode($response);
                exit;
            } else {
                // 插入失败
                $response = array(
                    'code' => -1,
                    'msg' => '系统出错，请稍后再试！'
                );
                echo json_encode($response);
                exit;
            }
        }
    } else {
        // system表中没有type为xitong的数据
        $response = array('code' => -3, 'msg' => '系统数据不存在');
        echo json_encode($response);
        exit;
    }
    
}

// 处理提交的支付宝表单数据
if (isset($_POST['alipay_name']) && isset($_POST['alipay_acc'])) {
    // var_dump($_POST);
    $alipay_name = $_POST['alipay_name'];
    $alipay_acc = $_POST['alipay_acc'];


    $query = "UPDATE proxy SET proxy_alipay='$alipay_name', proxy_alipay_acc='$alipay_acc' WHERE proxy_acc='$proxy_acc'";
    $result = mysqli_query($con, $query);
    
     if ($result) {
            // 更新成功
                $response = array(
                    'code' => 2,
                    'msg' => '绑定成功！'
                );
                echo json_encode($response);
                exit;
            } else {
                // 更新失败
                $response = array(
                    'code' => -1,
                    'msg' => '绑定失败，请稍后再试！'
                );
                echo json_encode($response);
                exit;
            }
}

// 处理提交的银行表单数据
if (isset($_POST['bank_name'])) {
    
    $bank_name = $_POST['bank_name'];
    $bank_acc = $_POST['bank_acc'];
    $proxy_bank_name = $_POST['proxy_bank_name'];
    $proxy_bank_zh = $_POST['proxy_bank_zh'];
    
     // 调试输出
    // echo "姓名: " . $bank_name . "<br>";
    // echo "卡号: " . $bank_acc . "<br>";
    // echo "银行: " . $proxy_bank_name . "<br>";
    // echo "支行: " . $proxy_bank_zh . "<br>";
    
    // 在这里处理银行表单的数据
    $query = "UPDATE proxy SET proxy_yihname='$bank_name', proxy_yhnum='$bank_acc', proxy_bank_name='$proxy_bank_name', proxy_bank_zh='$proxy_bank_zh' WHERE proxy_acc='$proxy_acc'";
    $result = mysqli_query($con, $query);
    if ($result) {
            // 更新成功
                $response = array(
                    'code' => 2,
                    'msg' => '绑定成功！'
                );
                echo json_encode($response);
                exit;
            } else {
                // 更新失败
                $response = array(
                    'code' => -1,
                    'msg' => '绑定失败，请稍后再试！'
                );
                echo json_encode($response);
                exit;
            }
    }


?>
